package com.undsf.despot.modles;

import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.basic.IntConverter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Arathi on 2016/10/3.
 */
public class Unit {
    public static final int RELIVE_HP_RESET_TYPE_ONE = 1;
    public static final int RELIVE_HP_RESET_TYPE_HALF = 2;
    public static final int RELIVE_HP_RESET_TYPE_FULL = 3;

    protected int id;
    protected String name;
    protected String suffix;
    protected int hpMax;
    protected int mpMax;
    protected int hp;
    protected int mp;
    protected int attack;
    protected int defense;
    protected int speed;
    protected int evasion;
    protected int intellect;
    protected int exp;
    protected int gold;
    protected Map<String, Integer> resistances;

    protected List<Skill> skills;
    protected List<Item> items;

    protected Map<String, Buff> buffs;

    protected Team team;
    protected Force force;

    public Unit() {
        init();
    }

    public void init() {
        resistances = new HashMap<String, Integer>();
        buffs = new HashMap<String, Buff>();
        suffix = "";
    }

    public int changeHp(int delta) {
        int nextHp = hp + delta;
        if (nextHp > hpMax) nextHp = hpMax;
        if (nextHp < 0) nextHp = 0;
        int realDelta = nextHp - hp;
        hp = nextHp;
        return realDelta;
    }

    public int changeMp(int delta) {
        int nextMp = mp + delta;
        if (nextMp > hpMax) nextMp = hpMax;
        if (nextMp < 0) nextMp = 0;
        int realDelta = nextMp - mp;
        mp = nextMp;
        return realDelta;
    }

    public String getName() {
        return name+suffix;
    }

    public int getAttack() {
        int atkSum = attack;
        return atkSum;
    }

    public int getDefense() {
        int defSum = defense;
        return defSum;
    }

    public Buff getBuff(String buffCode) {
        return buffs.get(buffCode);
    }

    public boolean checkBuff(String code) {
        if (buffs.containsKey(code)) {
            return true;
        }
        return false;
    }

    public boolean putBuff(Buff buff) {
        buffs.put(buff.key, buff);
        return true;
    }

    public boolean removeBuff(String code) {
        if (buffs.containsKey(code)) {
            buffs.remove(code);
            return true;
        }
        return false;
    }

    public Item getItemByCode(String code) {
        return null;
    }

    public Skill getSkillByCode(String code) {
        // TODO 目前找到第一个就行
        for (Skill skill : skills) {
            if (skill.spell.equals(code)) {
                return skill;
            }
        }
        return null;
    }

    /**
     * 住进Inn时，恢复HP和MP
     */
    public void resetHpMp() {
        if (hp>0) {
            hp = hpMax;
            mp = mpMax;
        }
    }

    /**
     * 复活（教堂、咒文、世界树之叶）
     */
    public boolean relive(int hpResetType) {
        switch (hpResetType) {
            case RELIVE_HP_RESET_TYPE_ONE:
                hp = 1;
                break;
            case RELIVE_HP_RESET_TYPE_HALF:
                hp = (int)Math.ceil(hpMax/2.0);
                break;
            case RELIVE_HP_RESET_TYPE_FULL:
                hp = hpMax;
                break;
            default:
                return false;
        }
        return true;
    }

    /**
     * 驱毒
     */
    @Deprecated
    public boolean detoxication() {
        if (buffs.containsKey("poison")) {
            Buff poisonDebuff = buffs.get("poison");
            buffs.remove("poison");
            return true;
        }
        return false;
    }

    public Command selectCommand() {
        Command command = new Command(this);
        command.targets = null;
        command.action = null;
        if (checkBuff("sleep")) {
            command.action = "wait";
        }
        return command;
    }

    public int getResistance(String property) {
        if (property == null || property.isEmpty()) return 0;
        if (resistances.containsKey(property)) {
            int resist = resistances.get(property);
            return resist;
        }
        return 0;
    }

    // --------------
    public int getHp() {
        return hp;
    }
    public int getMp() {
        return mp;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public int getHpMax() {
        return hpMax;
    }

    public void setHpMax(int hpMax) {
        this.hpMax = hpMax;
    }

    public int getMpMax() {
        return mpMax;
    }

    public void setMpMax(int mpMax) {
        this.mpMax = mpMax;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public void setMp(int mp) {
        this.mp = mp;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getEvasion() {
        return evasion;
    }

    public void setEvasion(int evasion) {
        this.evasion = evasion;
    }

    public int getIntellect() {
        return intellect;
    }

    public void setIntellect(int intellect) {
        this.intellect = intellect;
    }

    public int getExp() {
        return exp;
    }

    public void setExp(int exp) {
        this.exp = exp;
    }

    public int getGold() {
        return gold;
    }

    public void setGold(int gold) {
        this.gold = gold;
    }

    public Map<String, Integer> getResistances() {
        return resistances;
    }

    public void setResistances(Map<String, Integer> resistances) {
        this.resistances = resistances;
    }

    public List<Skill> getSkills() {
        return skills;
    }

    public void setSkills(List<Skill> skills) {
        this.skills = skills;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public Map<String, Buff> getBuffs() {
        return buffs;
    }

    public void setBuffs(Map<String, Buff> buffs) {
        this.buffs = buffs;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    public Force getForce() {
        return force;
    }

    public void setForce(Force force) {
        this.force = force;
    }
}
